package com.example.biometrics;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
import java.util.HashMap;
import java.util.Map;
import com.android.microdroid.biologic.IBiologicService;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "example.com/channel";
    private String action;
    private IBiologicService testService = null;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (service != null) {
                System.out.println("LJH:com.example.biometrics: start testService");
                testService = IBiologicService.Stub.asInterface(service);
            } else {
                System.out.println("LJH:com.example.biometrics: start testService error!!!!!!!!!!!");
            }
        } 

        @Override
        public void onServiceDisconnected(ComponentName name) {
            testService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        action = intent.getAction();

        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler((call, result) -> {
                    if (call.method.equals("getIntentAction")) {
                        Map<String, String> resultMap = new HashMap<>();
                        resultMap.put("action", action);
                        String faceId = intent.getStringExtra("face_id");
                        resultMap.put("face_id", faceId);
                        result.success(resultMap);
                    } else if (call.method.equals("setResultAndClose")) {
                        int resultCode = call.argument("resultCode");
                        String resultData = call.argument("resultData");
                        Intent resultIntent = new Intent();
                        resultIntent.putExtra("result", resultData);
                        setResult(RESULT_OK, resultIntent);
                        finish();
                    } else if (call.method.equals("writeFaceFeature")) {
                        int id = call.argument("id");
                        String feature = call.argument("feature");
                        int ret = writeFaceFeature(feature, id);
                        result.success(ret);
                    } else if (call.method.equals("matchFaceFeature")) {
                        Integer id = call.argument("id");
                        String feature = call.argument("feature");
                        int ret = -100;
                        if (id == null) {
                            ret = recognizeFaceFeature(feature);
                        } else {
                            ret = matchFaceFeature(feature, id);
                        }
                        result.success(ret);
                    } else if (call.method.equals("clearUserFaceFeatures")) {
                        Integer id = call.argument("id");
                        int ret = clearUserFaceFeatures(id);
                        result.success(ret);
                    } else if (call.method.equals("listall")){
                        String ret = listall();
                        result.success(ret);
                    }
                });

        initService();
    }

    private void initService() {
        Intent vmIntent = new Intent();
        vmIntent.setComponent(new ComponentName("com.android.microdroid.biologic", "com.android.microdroid.biologic.core.BiologicService"));
        bindService(vmIntent, connection, Context.BIND_AUTO_CREATE);
    }

    private int writeFaceFeature(String featureData, int faceId) {
         int ret = -100;

         if (testService != null) {
             try {
                 System.out.println("LJH:com.example.biometrics: writeFaceFeature!!!!!!" + faceId);
                 System.out.println("LJH:com.example.biometrics: writeFaceFeature!!!!!!" + featureData);
                 ret = testService.writeFaceFeature(featureData, faceId);
             } catch (RemoteException e) {
                 System.out.println("LJH:com.example.biometrics: writeFaceFeature error!!!!!!");

                 e.printStackTrace();
             }
         } else {
             System.out.println("LJH:com.example.biometrics: testService == null");
         }
        System.out.println("LJH:com.example.biometrics: writeFaceFeature ret = " + ret);

         return ret;
    }

    private int matchFaceFeature(String featureData, int matchedFaceId) {
         int ret = -100;

         if (testService != null) {
             try {
                 System.out.println("LJH:com.example.biometrics: matchFaceFeature!!!!!!");
                 ret = testService.matchFaceFeature(featureData, matchedFaceId);
             } catch (RemoteException e) {
                 System.out.println("LJH:com.example.biometrics: matchFaceFeature error!!!!!!");
                 e.printStackTrace();
             }
         } else {
             System.out.println("LJH:com.example.biometrics: testService == null");
         }

         // 打印
         System.out.println("LJH:com.example.biometrics: matchFaceFeature ret = " + ret);

         return ret;
    }

    private int recognizeFaceFeature(String featureData) {
         int ret = -100;

         if (testService != null) {
             try {
                 System.out.println("LJH:com.example.biometrics: recognizeFaceFeature!!!!!!");
                 ret = testService.recognizeFaceFeature(featureData);
             } catch (RemoteException e) {
                 System.out.println("LJH:com.example.biometrics: recognizeFaceFeature error!!!!!!");
                 e.printStackTrace();
             }
         } else {
             System.out.println("LJH:com.example.biometrics: testService == null");
         }

        System.out.println("LJH:com.example.biometrics: recognizeFaceFeature ret = " + ret);
         return ret;
    }

    private int clearUserFaceFeatures(int faceId) {
         int ret = -100;

         if (testService != null) {
             try {
                 System.out.println("LJH:com.example.biometrics: clearUserFaceFeatures!!!!!!");
                 ret = testService.clearUserFaceFeatures(faceId);
             } catch (RemoteException e) {
                 System.out.println("LJH:com.example.biometrics: clearUserFaceFeatures error!!!!!!");
                 e.printStackTrace();
             }
         } else {
             System.out.println("LJH:com.example.biometrics: testService == null");
         }
        System.out.println("LJH:com.example.biometrics: clearUserFaceFeatures ret = " + ret);

         return ret;
    }

    private String listall() {
        String list = "";
        if(testService!=null) {
            try {
                System.out.println("LJH:com.example.biometrics: listall!!!!!!");
                list = testService.listall();
            } catch (RemoteException e) {
                System.out.println("LJH:com.example.biometrics: listall error!!!!!!");
                e.printStackTrace();
            }
        } else {
            System.out.println("LJH:com.example.biometrics: testService == null");
        }
        System.out.println("LJH:com.example.biometrics: listall ret = " + list);
        return list;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unbindService(connection);
        if (testService != null) {
//            unbindService(connection);
            testService = null;
        }
    }
}
